
//
//  ZJStudyCoureSwithView.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/16.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJStudyCoureSwithView.h"

@interface ZJStudyCoureSwithView ()

/** 简介 */
@property (nonatomic, strong) UIButton *synopsis;
/** 评论 */
@property (nonatomic, strong) UIButton *comment;
/** 笔记 */
@property (nonatomic, strong) UIButton *note;
/** 红色下线 */
@property (nonatomic, strong) UIView *underLine;
/** 分割线 */
@property (nonatomic, strong) UILabel *spacrline;

@end

@implementation ZJStudyCoureSwithView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.synopsis];
        [self addSubview:self.comment];
        [self addSubview:self.note];
        [self addSubview:self.spacrline];
        [self addSubview:self.underLine];
        
        self.synopsis.selected = YES;
    }
    return self;
}


- (void)selectNumberString:(NSString *)string{
    if ([string isEqualToString:@"1"]) {
        self.synopsis.selected = YES;
        self.comment.selected = NO;
        self.note.selected = NO;
        __weak typeof(self) weakSelf = self;

        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.underLine.centerX = self.synopsis.centerX;
        }];
        
    }else if ([string isEqualToString:@"2"]) {
        self.synopsis.selected = NO;
        self.comment.selected = YES;
        self.note.selected = NO;
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.underLine.centerX = self.comment.centerX;
        }];
    }else if ([string isEqualToString:@"3"]) {
        self.synopsis.selected = NO;
        self.comment.selected = NO;
        self.note.selected = YES;
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.underLine.centerX = self.note.centerX;
        }];
    }
}

#pragma mark - 点击事件
- (void)action_synopsis:(UIButton *)sender{
    if (!sender.selected) {
        [self selectNumberString:@"1"];
        if (self.delegate) {
            [self.delegate ZJStudyCoureSwithViewDelegate_synopsisClick];
        }
    }
}
- (void)action_comment:(UIButton *)sender{
    if (!sender.selected) {
        [self selectNumberString:@"2"];
        if (self.delegate) {
            [self.delegate ZJStudyCoureSwithViewDelegate_commentClick];
        }
    }
}
- (void)action_note:(UIButton *)sender{
    if (!sender.selected) {
        [self selectNumberString:@"3"];
        if (self.delegate) {
            [self.delegate ZJStudyCoureSwithViewDelegate_noteClick];
        }
    }
}


#pragma mark - Setter


#pragma mark - Getters
- (UIButton *)synopsis{
    if (_synopsis == nil){
        _synopsis = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.width / 3, self.height)];
        [_synopsis setTitle:@"简介" forState:UIControlStateNormal];
        [_synopsis setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        _synopsis.titleLabel.font = [UIFont titleTextFont];
        [_synopsis setTitleColor:[UIColor themeColor] forState:UIControlStateSelected];
        [_synopsis addTarget:self action:@selector(action_synopsis:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _synopsis;
}

- (UIButton *)comment{
    if (_comment == nil){
        _comment = [[UIButton alloc]initWithFrame:CGRectMake(self.synopsis.right, 0, self.width / 3, self.height)];
        [_comment setTitle:@"评论" forState:UIControlStateNormal];
        [_comment setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        _comment.titleLabel.font = [UIFont titleTextFont];
        [_comment setTitleColor:[UIColor themeColor] forState:UIControlStateSelected];
        [_comment addTarget:self action:@selector(action_comment:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _comment;
}

- (UIButton *)note{
    if (_note == nil){
        _note = [[UIButton alloc]initWithFrame:CGRectMake(self.comment.right, 0, self.width / 3, self.height)];
        [_note setTitle:@"笔记" forState:UIControlStateNormal];
        _note.titleLabel.font = [UIFont titleTextFont];
        [_note setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        [_note setTitleColor:[UIColor themeColor] forState:UIControlStateSelected];
        [_note addTarget:self action:@selector(action_note:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _note;
}

- (UIView *)underLine{
    if (_underLine == nil){
        _underLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 2, self.width / 3  - 40 * [UIScreen zj_screenWScale], 1.5)];
        _underLine.backgroundColor = [UIColor themeColor];
        _underLine.centerX = self.synopsis.centerX;
    }
    return _underLine;
}

- (UILabel *)spacrline{
    if (_spacrline == nil){
        _spacrline = [[UILabel alloc] initWithFrame:CGRectMake(0, self.height - 0.5, self.width, 0.5)];
        _spacrline.backgroundColor = [UIColor zj_colorWithHexString:@"dedede"];
    }
    return _spacrline;
}

@end
